package com.act.moudles.act.api;

import com.act.moudles.act.pojo.TaskRunPojo;
import com.act.moudles.act.service.TaskRunService;
import com.act.moudles.user.entity.User;
import com.act.moudles.user.service.SecretKeyService;
import com.act.utils.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: mengxiangzhong
 * @date: 2023/5/29 9:06
 * @TODO:
 **/
@RequestMapping("api/act")
@Api(tags = "流程API")
@RestController
public class ActivityApis {

    @Resource
    private SecretKeyService secretKeyService;

    @Resource
    private TaskRunService taskRunService;

    @PostMapping("startFlow")
    @ApiOperation("启动流程")
    public DataResult startFlow(@RequestBody TaskRunPojo pojo){
        if(StringUtils.isEmpty(pojo.getSecretKey())){
            return DataResult.fail("密钥不能为空！");
        }
        User user = secretKeyService.findUserBySecretKey(pojo.getSecretKey());
        if(user == null){
            return DataResult.fail("密钥错误");
        }
        try {
            return DataResult.success("查询成功！",taskRunService.startFlow(pojo,user));
        }catch (Exception e){
            return DataResult.fail(e.getMessage());
        }
    }

    @PostMapping("compileTask")
    @ApiOperation("任务执行完成！")
    public DataResult compileTask(@RequestBody TaskRunPojo pojo){
        if(StringUtils.isEmpty(pojo.getSecretKey())){
            return DataResult.fail("密钥不能为空！");
        }
        User user = secretKeyService.findUserBySecretKey(pojo.getSecretKey());
        if(user == null){
            return DataResult.fail("密钥错误");
        }
        try {
            taskRunService.compileTask(pojo,user);
            return DataResult.success("处理完成");
        }catch (Exception e){
            return DataResult.fail("处理异常");
        }
    }







}
